package GUI;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
import ModelLayer.*;

public class PersonTable extends AbstractTableModel {

	private String[] columnNames = { "PhoneNumber", "CustomerType", "Name",
			"Address", "ZipCode", "City", "Email", "CompanyName", "CVR Number",
			"JobType" };

	private ArrayList<Person> data;

	//Constructor
	public PersonTable() 
	{
		data = new ArrayList<Person>();

	}

	//Sets the persons
	public void setPersons(ArrayList<Person> persons) 
	{
		data = persons;
		fireTableDataChanged();
	}
	
	//Sets the customers
	public void setCustomers(ArrayList<Customer> customers)
	{
		data = new ArrayList<Person>();
		data.addAll(customers);
		fireTableDataChanged();
	}
	
	//Sets the employees
	public void setEmployees(ArrayList<Employee> employees)
	{
		data = new ArrayList<Person>();
		data.addAll(employees);
		fireTableDataChanged();
	}
	
	//Set a single person
	public void setPerson(Person person)
	{
		data = new ArrayList<Person>();
		data.add(person);
		fireTableDataChanged();
	}

	//Method does so the table cannot be edited
	public boolean isCellEditable(int row, int col) 
	{
		return false;
	}

	//Returns the column count
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	//Returns the row count
	@Override
	public int getRowCount() {
		return data.size();
	}

	//Returns the column name
	@Override
	public String getColumnName(int col) {
		return columnNames[col];
	}

	//Returns the person attributes in the table
	@Override
	public Object getValueAt(int row, int col) {

		Person person = data.get(row);

		if (person.getClass().equals(Employee.class)) {
			switch (col) {

			case 0:
				return person.getPhoneNumber();
			case 1:
				return null;
			case 2:
				return person.getName();
			case 3:
				return person.getAdress();
			case 4:
				return person.getZipCode();
			case 5:
				return person.getCity();
			case 6:
				return person.getEmail();
			case 7:
				return null;
			case 8:
				return null;
			case 9:
				return ((Employee) person).getJobType();

			default:
				return null;

			}

		} else if (person.getClass().equals(Customer.class)) {
			switch (col) {

			case 0:
				return person.getPhoneNumber();
			case 1:
				return ((Customer) person).getCustomerType();
			case 2:
				return person.getName();
			case 3:
				return person.getAdress();
			case 4:
				return person.getZipCode();
			case 5:
				return person.getCity();
			case 6:
				return person.getEmail();
			case 7:
				return ((Customer) person).getCompanyName();
			case 8:
				return ((Customer) person).getCvrNr();
			case 9:
				return null;

			default:
				return null;

			}

		}
		return null;
	}
	
	public Person getPerson(int row)
	{
		return data.get(row);
	}

}
